/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.ui;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import org.gwtportlets.portlet.client.WidgetFactory;
import org.gwtportlets.portlet.client.ui.Portlet;
import org.gwtportlets.portlet.smartgwt.client.DataTransferObject;
import org.gwtportlets.portlet.smartgwt.client.SmartPortlet;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletFactory;
import za.org.foobar.famon.frontend.client.Famon;
import za.org.foobar.famon.frontend.client.FamonPage;
import za.org.foobar.famon.frontend.client.dto.FailureDto;
import za.org.foobar.famon.frontend.client.dto.LoggingEventDto;
import za.org.foobar.famon.frontend.client.dto.StackTraceElementDto;
import za.org.foobar.famon.frontend.client.dto.ThreadDto;

/**
 *
 */
public class FailurePortlet extends SmartPortlet {
    private FailureWidget failureWidget;

    public FailurePortlet() {
        FamonPage page = Famon.getInstance().getPage();
        String id = page.getParameter("id");
        if (id == null) {
            initWidget(new Label("No page parameters provided. Expected id."));
            return;
        }
        failureWidget = (FailureWidget)Canvas.getById(FailureWidget.getComponentId(id));
        if (failureWidget == null) {
            failureWidget = new FailureWidget(this, id);
        }

        initWidget(failureWidget);
    }

    public WidgetFactory createWidgetFactory() {
        return new Factory();
    }

    public static class Factory extends SmartPortletFactory<FailurePortlet> {

        public Factory() {
        }

        public Factory(Portlet p) {
            super(p);
        }

        public FailurePortlet createWidget() {
            return new FailurePortlet();
        }

        public DataTransferObject createDto() {
            if (FailureDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new FailureDto();
            } else if (ThreadDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new ThreadDto();
            } else if (StackTraceElementDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new StackTraceElementDto();
            } else if (LoggingEventDto.DATA_SOURCE_ID.equals(dataSourceId)) {
                return new LoggingEventDto();
            }
            return null;
        }
    }
}
